package org.tis.tools.abf.module.ac.controller;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.tis.tools.abf.module.ac.controller.request.AcDeleteListRequest;
import org.tis.tools.abf.module.ac.controller.request.AcFuncParameterRequest;
import org.tis.tools.abf.module.ac.controller.request.AcFuncSonRequest;
import org.tis.tools.abf.module.ac.controller.request.AcFuncUpdateRequest;
import org.tis.tools.abf.module.ac.entity.AcFunc;
import org.tis.tools.abf.module.ac.entity.enums.FuncType;
import org.tis.tools.abf.module.ac.service.IAcFuncService;
import org.tis.tools.core.utils.StringUtil;
import org.tis.tools.core.web.controller.BaseController;
import org.tis.tools.core.web.vo.SmartPage;
import org.tis.tools.model.common.ResultVO;
import org.tis.tools.model.log.enums.OperateType;
import org.tis.tools.starter.operatelog.annotation.OperateLog;

import javax.validation.constraints.NotBlank;

/**
 * acFunc的Controller类
 *
 * @author Auto Generate Tools
 * @date 2018/04/23
 */
@RestController
@RequestMapping("/acFunc")
@Validated
@Slf4j
public class AcFuncController extends BaseController<AcFunc>  {

    @Autowired
    private IAcFuncService acFuncService;

    @ApiOperation(value = "新增功能",notes = "传入AcFuncSonRequest类作为参数")
    @OperateLog(type = OperateType.ADD, desc = "新增功能")
    @PostMapping
    public ResultVO add(@RequestBody @Validated @ApiParam(value = "acFuncSonRequest",name = "传入的AcFuncSonRequest类",required = true) AcFuncSonRequest acFuncSonRequest){

        if (FuncType.BEHAVE.toString().equals(acFuncSonRequest.getFuncType().toString())){
            if (StringUtil.isEmpty(acFuncSonRequest.getServiceEntry())){
                return ResultVO.error("404", "路径参数不能为空！");
            }
        }
        if (StringUtil.isNotEmpty(acFuncSonRequest.getServiceEntry())){
            if (acFuncService.existPathParameter("",acFuncSonRequest.getServiceEntry())){
                return ResultVO.error("404", "路径参数已经存在！");
            }
        }
         acFuncService.creatFunc(acFuncSonRequest.getGuidApp(),acFuncSonRequest.getFuncType(),acFuncSonRequest.getFuncCode(),
                acFuncSonRequest.getFuncName(), acFuncSonRequest.getDisplayOrder(),acFuncSonRequest.getFuncDesc(),
                acFuncSonRequest.getGuidFunc(),acFuncSonRequest.getIsopen(),acFuncSonRequest.getIscheck(),
                 acFuncSonRequest.getUiEntry(),acFuncSonRequest.getServiceEntry());

        return ResultVO.success("新增成功!");
    }

    @ApiOperation(value = "修改功能",notes = "传入AcFuncUpdateRequest类作为参数")
    @OperateLog(type = OperateType.UPDATE, desc = "修改功能")
    @PutMapping
    public ResultVO update(@RequestBody @Validated @ApiParam(value = "acFuncUpdateRequest",name = "传入的AcFuncUpdateRequest类",required = true) AcFuncUpdateRequest acFuncUpdateRequest) {

        AcFunc acFunc = acFuncService.selectById(acFuncUpdateRequest.getGuid());
        if (acFunc == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        if (StringUtil.isNotEmpty(acFuncUpdateRequest.getServiceEntry())){
            if (acFuncService.existPathParameter(acFuncUpdateRequest.getGuid(),acFuncUpdateRequest.getServiceEntry())){
                return ResultVO.error("404", "路径参数已经存在！");
            }
        }
        AcFunc acFunc1 = acFuncService.changeFunc(acFuncUpdateRequest.getGuid(),acFuncUpdateRequest.getGuidApp(),acFuncUpdateRequest.getFuncType(), acFuncUpdateRequest.getFuncCode(),acFuncUpdateRequest.getFuncName(),acFuncUpdateRequest.getFuncDesc(),acFuncUpdateRequest.getIsopen(),acFuncUpdateRequest.getIscheck(),acFuncUpdateRequest.getDisplayOrder(),acFuncUpdateRequest.getGuidFunc(),
                acFuncUpdateRequest.getUiEntry(),acFuncUpdateRequest.getServiceEntry());
        return ResultVO.success("修改成功!",acFunc1);
    }

    @ApiOperation(value = "批量删除功能",notes = "传入AcDeleteListRequest类作为参数")
    @OperateLog(type = OperateType.DELETE,desc = "批量删除功能")
    @PostMapping("/batchDelete")
    public ResultVO batchDelete(@RequestBody @Validated @ApiParam(value = "listRequest",name = "传入的AcDeleteListRequest类",required = true) AcDeleteListRequest listRequest) {

        acFuncService.moveBatchFunc(listRequest);
        return ResultVO.success("删除成功!");
    }

    @ApiOperation(value = "删除功能",notes = "传入功能行为GUID作为参数")
    @OperateLog(type = OperateType.DELETE,desc = "删除功能")
    @DeleteMapping("/{id}")
    public ResultVO delete(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(value = "id",name = "功能行为GUID",required = true) String id) {
        AcFunc acFunc = acFuncService.selectById(id);
        if (acFunc == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        acFuncService.moveFunc(id);
        return ResultVO.success("删除成功!");
    }

    @ApiOperation(value = "查询功能",notes = "传入功能行为GUID作为参数")
    @GetMapping("/{id}")
    public ResultVO detail(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(value = "id",name = "功能行为GUID",required = true) String id) {
        AcFunc acFunc = acFuncService.selectById(id);
        if (acFunc == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        return ResultVO.success("查询成功", acFunc);
    }

    @ApiOperation(value = "分页查询功能行为列表",notes = "传入分页数据(page,condition)作为参数")
    @PostMapping("/list/{id}")
    public ResultVO list(@RequestBody @Validated @ApiParam(value = "page",name = "传入的分页数据(page,condition)",required = true) SmartPage<AcFunc> page,@PathVariable @NotBlank(message = "id不能为空") @ApiParam(value = "id",name = "功能行为GUID",required = true) String id ) {
        return  ResultVO.success("查询成功", acFuncService.queryPageById(page.getPageObj(), page.getConditionWrapper(),id));
    }

    @ApiOperation(value = "查询所有功能",notes = "不需要传入任何参数")
    @GetMapping("/queryAll")
    public ResultVO queryAll(){
        return ResultVO.success("查询成功",acFuncService.queryAll());
    }

    @ApiOperation(value = "根据应用ID查询功能(分页)",notes = "传入分页(page,condition),和应用ID作为参数")
    @PostMapping("/pageQueryFuncByAppId/{appId}")
    public ResultVO pageQueryFuncByAppId(@RequestBody @Validated @ApiParam(value = "page",name = "传入的分页数据(page,condition)",required = true) SmartPage<AcFunc> page,@PathVariable @NotBlank(message = "应用ID不能为空") @ApiParam(value = "appId",name = "应用GUID",required = true) String appId){
        return ResultVO.success("查询成功",acFuncService.pageQueryFuncByAppId(page.getPageObj(),page.getConditionWrapper(),appId));
    }

    @ApiOperation(value = "查询功能下所有行为(分页)",notes = "传入分页(page,condition).和功能GUID作为参数")
    @PostMapping("/pageQueryBehaveByFuncId/{funcId}")
    public ResultVO pageQueryBehaveByFuncId(@RequestBody @Validated @ApiParam(value = "page",name = "传入的分页数据(page,condition)",required = true) SmartPage<AcFunc> page,@PathVariable @NotBlank(message = "功能ID不能为空") @ApiParam(value = "funcId",name = "功能GUID",required = true) String funcId){
        return ResultVO.success("查询成功",acFuncService.pageQueryBehaveByFuncId(page.getPageObj(),page.getConditionWrapper(),funcId));
    }

    @ApiOperation(value = "开通功能",notes = "传入需要开通功能的GUID作为参数")
    @OperateLog(type = OperateType.UPDATE,desc = "开通功能")
    @PutMapping("/openFunc/{id}")
    public ResultVO openFunc(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(value = "id",name = "功能GUID",required = true) String id) {

        AcFunc acFunc = acFuncService.selectById(id);
        if (acFunc == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除!");
        }
        acFunc = acFuncService.openFunc(acFunc);

        return ResultVO.success("开通成功！",acFunc);
    }

    @ApiOperation(value = "停用功能",notes = "传入需要开通功能的GUID作为参数")
    @OperateLog(type = OperateType.UPDATE,desc = "停用功能")
    @PutMapping("/closeFunc/{id}")
    public ResultVO closeFunc(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(value = "id",name = "功能GUID",required = true) String id) {

        AcFunc acFunc = acFuncService.selectById(id);
        if (acFunc == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除!");
        }
        acFunc = acFuncService.closeFunc(acFunc);
        return ResultVO.success("停用成功！",acFunc);
    }

    @ApiOperation(value = "根据应用id查询功能树",notes = "传入应用的GUID作为参数")
    @GetMapping("/funcTreeByApp/{appId}")
    public ResultVO queryFuncTreeByApp(@PathVariable @NotBlank(message = "应用id不能为空") @ApiParam(value = "appId",name = "应用GUID",required = true) String appId){
        return ResultVO.success("查询成功",acFuncService.queryFuncTreeByApp(appId));
    }

    @ApiOperation(value = "根据应用和功能id查询子功能树",notes = "传入应用GUID和功能GUID作为参数")
    @GetMapping("/funcTreeByAppFunc/{appId}/{funcId}")
    public ResultVO queryTreeByAppFunc(@PathVariable @NotBlank(message = "应用id不能为空") @ApiParam(value = "appId",name = "应用GUID",required = true) String appId,@PathVariable @NotBlank(message = "功能id不能为空") @ApiParam(value = "funcId",name = "功能GUID",required = true) String funcId){
        return ResultVO.success("查询成功",acFuncService.queryFuncTreeByAppFunc(appId,funcId));
    }

    @ApiOperation(value = "根据应用和功能id查询行为列表",notes = "传入应用GUID和功能GUID作为参数")
    @GetMapping("/behaveTreeByAppFunc/{appId}/{funcId}")
    public ResultVO queryBehaveByAppFunc(@PathVariable @NotBlank(message = "应用id不能为空") @ApiParam(value = "appId",name = "应用GUID",required = true) String appId,@PathVariable @NotBlank(message = "功能id不能为空") @ApiParam(value = "funcId",name = "功能GUID",required = true) String funcId){
        return ResultVO.success("查询成功",acFuncService.queryBehaveTreeByAppFunc(appId,funcId));
    }

    @ApiOperation(value = "根据应用ID查询功能",notes = "传入应用GUID作为参数")
    @GetMapping("/{appId}/getFuncByApp")
    public ResultVO getFuncByApp(@PathVariable @NotBlank(message = "应用GUID不能为空") @ApiParam(value = "appId",name = "应用GUID",required = true) String appId){
        return ResultVO.success("查询成功",acFuncService.queryFuncByApp(appId));
    }

    @ApiOperation(value = "校验路径参数是否存在",notes = "传入功能GUID和路径参数作为参数")
    @PostMapping("/existPathParameter")
    public ResultVO existPathParameter(@RequestBody @Validated @ApiParam(value = "AcFuncParameterRequest",name =
            "传入的AcFuncParameterRequest类",required =true) AcFuncParameterRequest request){
        return ResultVO.success("查询成功",acFuncService.existPathParameter(request.getGuid(),request.getServiceEntry()));
    }

    /** 行为接口 */

    @ApiOperation(value = "新增行为",notes = "传入AcFuncSonRequest类作为参数")
    @OperateLog(type = OperateType.ADD, desc = "新增行为")
    @PostMapping("/addBehave")
    public ResultVO addBehave(@RequestBody @Validated @ApiParam(value = "acFuncSonRequest",name = "传入的AcFuncSonRequest类",required = true) AcFuncSonRequest acFuncSonRequest){

        if (FuncType.BEHAVE.toString().equals(acFuncSonRequest.getFuncType().toString())){
            if (StringUtil.isEmpty(acFuncSonRequest.getServiceEntry())){
                return ResultVO.error("404", "路径参数不能为空！");
            }
        }
        if (StringUtil.isNotEmpty(acFuncSonRequest.getServiceEntry())){
            if (acFuncService.existPathParameter("",acFuncSonRequest.getServiceEntry())){
                return ResultVO.error("404", "路径参数已经存在！");
            }
        }
        acFuncService.creatFunc(acFuncSonRequest.getGuidApp(),acFuncSonRequest.getFuncType(),acFuncSonRequest.getFuncCode(),
                acFuncSonRequest.getFuncName(), acFuncSonRequest.getDisplayOrder(),acFuncSonRequest.getFuncDesc(),
                acFuncSonRequest.getGuidFunc(),acFuncSonRequest.getIsopen(),acFuncSonRequest.getIscheck(),
                acFuncSonRequest.getUiEntry(),acFuncSonRequest.getServiceEntry());

        return ResultVO.success("新增成功!");
    }


    @ApiOperation(value = "修改行为",notes = "传入AcFuncUpdateRequest类作为参数")
    @OperateLog(type = OperateType.UPDATE, desc = "修改行为")
    @PutMapping("/updateBehave")
    public ResultVO changeBehave(@RequestBody @Validated @ApiParam(value = "acFuncUpdateRequest",name = "传入的AcFuncUpdateRequest类",required = true) AcFuncUpdateRequest acFuncUpdateRequest) {

        AcFunc acFuncQue = acFuncService.selectById(acFuncUpdateRequest.getGuid());
        if (null == acFuncQue) {
            return ResultVO.error("404", "找不到对应记录或已经被删除");
        }
        if (StringUtil.isNotEmpty(acFuncUpdateRequest.getServiceEntry())){
            if (acFuncService.existPathParameter(acFuncUpdateRequest.getGuid(),acFuncUpdateRequest.getServiceEntry())){
                return ResultVO.error("404", "路径参数已经存在");
            }
        }
        AcFunc acFunc1 = acFuncService.changeFunc(acFuncUpdateRequest.getGuid(),acFuncUpdateRequest.getGuidApp(),acFuncUpdateRequest.getFuncType(), acFuncUpdateRequest.getFuncCode(),acFuncUpdateRequest.getFuncName(),acFuncUpdateRequest.getFuncDesc(),acFuncUpdateRequest.getIsopen(),acFuncUpdateRequest.getIscheck(),acFuncUpdateRequest.getDisplayOrder(),acFuncUpdateRequest.getGuidFunc(), acFuncUpdateRequest.getUiEntry(),acFuncUpdateRequest.getServiceEntry());
        return ResultVO.success("修改成功!",acFunc1);
    }

    @ApiOperation(value = "批量删除行为",notes = "传入AcDeleteListRequest类作为参数")
    @OperateLog(type = OperateType.DELETE,desc = "批量删除行为")
    @PostMapping("/batchDeleteBehave")
    public ResultVO batchDeleteBehave(@RequestBody @Validated @ApiParam(value = "listRequest",name = "传入的AcDeleteListRequest类",required = true) AcDeleteListRequest listRequest) {

        acFuncService.moveBatchFunc(listRequest);
        return ResultVO.success("删除成功!");
    }

    @ApiOperation(value = "删除功能行为",notes = "传入功能行为GUID作为参数")
    @OperateLog(type = OperateType.DELETE,desc = "删除功能行为")
    @DeleteMapping("/{id}/deleteBehave")
    public ResultVO deleteBehave(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(value = "id",name = "功能行为GUID",required = true) String id) {
        AcFunc acFunc = acFuncService.selectById(id);
        if (acFunc == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        acFuncService.moveFunc(id);
        return ResultVO.success("删除成功!");
    }

}

